//
//  B_ReportListVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/9/1.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_ReportListVC.h"
#import "B_ReportListTVC.h"
#import "B_ReportListModel.h"

@interface B_ReportListVC ()
@property (nonatomic, strong) BaseTableView * mainView;

@end

@implementation B_ReportListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"举报"];
    self.view = self.mainView;
    [self getData];
}


- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_ReportListTVC" bundle:nil] forCellReuseIdentifier:@"B_ReportListTVC"];
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 63;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_ReportListModel * model = weSelf.dataArray[indexPath.row];
            B_ReportListTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_ReportListTVC" forIndexPath:indexPath];
            cell.B_titleLable.text = model.content;
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            B_ReportListModel * model = weSelf.dataArray[indexPath.row];
            [weSelf alertViewCenterWithTitle:@"温馨提示" message:@"您确定要举报吗?" buttonsTitles:@[@"确定", @"取消"] action:^(NSInteger index) {
                if (index == 0) {
                    // TODO:调用接口来举报
                    [weSelf reportUser:model];
                } else {
                    
                }
            }];
        };
        
    }
    return _mainView;
}

- (void) getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:@"/yearnfor/inter/report/selectReportList" parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"]isEqualToString:@"20010"]) {
            for (NSDictionary * dic in data[@"values"]) {
                B_ReportListModel * model = [B_ReportListModel mj_objectWithKeyValues:dic];
                [weSelf.dataArray addObject:model];
            }
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

/**
 TODO: 举报的action

 @param model 模型
 */
- (void) reportUser:(B_ReportListModel *) model {
    NSString * stringPath;
    if (self.type == B_ReportListVCTypeUser) {
        stringPath = [NSString stringWithFormat:@"/yearnfor/inter/report/addUserReport/%@/%@/%@", getUserId(), self.B_userID, model.id];
    } else if (self.type == B_ReportListVCTypeTopic) {
        stringPath = [NSString stringWithFormat:@"/yearnfor/inter/report/addTopicReport/%@/%@/%@", getUserId(), self.topicId, model.id];
    } else if (self.type == B_ReportListVCTypeActivity) {
        stringPath = [NSString stringWithFormat:@"/yearnfor/inter/report/addActivityReport/%@/%@/%@", getUserId(), self.activityId, model.id];
    }
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:stringPath parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20005"]) {
            [KeyWindow makeToast:@"您提交的举报信息我们将会尽快受理"];
            [weSelf.navigationController popViewControllerAnimated:YES];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
